package myJs.detailInfo

import cats.effect.IO
import japgolly.scalajs.react.ScalaFnComponent
import japgolly.scalajs.react.util.EffectCatsEffect._
import japgolly.scalajs.react.vdom.all._
import myJs.Implicits._
import myJs.api.Api
import myJs.cps.BootstrapTableCp
import myJs.cps.BootstrapTableCp.Column
import myJs.myPkg.Hooks._
import myJs.myPkg.reactBootstrap._
import myJs.myPkg.tabs._
import myJs.tool.Pojo._
import myJs.tool.Tool
import shared.pojo.Browse._
import upickle.default._

import scala.collection.immutable.SeqMap
import scala.scalajs.js

/** Created by yz on 28/11/2023
  */
object SnpCp {

  case class Props(id: String)

  val Component = ScalaFnComponent[Props] { props =>
    val (hasSnpSummary, setHasSnpSummary) = useState(true)
    val (hasCdsSnp, setHasCdsSnp) = useState(true)
    val (hasInterSnp, setHasInterSnp) = useState(true)
    val initTab = List("snpSummary", "cdsSnp", "interSnp")(0)
    val (tab, setTab) = useState(initTab)

    object FTool {

      def tabChange = (v: String) => {
        setTab(v)
      }

    }

    useEffect(
      {
        if (
          (!hasSnpSummary && tab == "snpSummary") || (!hasCdsSnp && tab == "cdsSnp") || (!hasInterSnp && tab == "interSnp")
        ) {
          val curTab = List(("snpSummary", hasSnpSummary), ("cdsSnp", hasCdsSnp), ("interSnp", hasInterSnp))
            .filter(_._2)
            .headOption
            .map(_._1)
            .getOrElse("")
          setTab(curTab)
        } else IO {}
      },
      List(hasSnpSummary, hasCdsSnp, hasInterSnp)
    )

    if (List(hasSnpSummary, hasCdsSnp, hasInterSnp).exists(x => x)) {
      div(
        h4("SNP"),
        Tabs(id = "tab", activeKey = tab, onSelect = FTool.tabChange, transition = false)(
          if (hasSnpSummary) {
            Tab(eventKey = "snpSummary", title = s"SNP Summary")(
              div(
                marginTop := 20.px,
                SnpSummaryCp(id = props.id, hasChange = setHasSnpSummary.toIO)
              )
            )
          } else EmptyVdom,
          if (hasCdsSnp) {
            Tab(eventKey = "cdsSnp", title = s"CDS SNP")(
              div(
                marginTop := 20.px,
                CdsSnpCp(id = props.id, hasChange = setHasCdsSnp.toIO)
              )
            )
          } else EmptyVdom,
          if (hasInterSnp) {
            Tab(eventKey = "interSnp", title = s"Intergenic SNP")(
              div(
                marginTop := 20.px,
                InterSnpCp(id = props.id, hasChange = setHasInterSnp.toIO)
              )
            )
          } else EmptyVdom
        )
      )
    } else EmptyVdom

  }

  def apply(id: String) = {
    Component(Props(id))
  }

}
